package com.example.jl.bethinner.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.jl.bethinner.R;

/**
 * Created by JL on 9/7/15.
 */


public class CustomView extends View {

    private float mBorderWidth;//线的宽度

    private int mBorderColor;
    private int rulerColor = 0x7DC0C0C0;

    private Bitmap bitmapBlue;

    private Paint mPaint;
    private Paint rulerPaint;
    /**
     * 圆环半径 Note: (Re)在onMeasure计算{@link #onMeasure(int, int)}
     */
    private RectF mBounds;
    private float width;
    private float height;
    float mRadians = 0f;//默认弧度
    private float timeRotate = calculateAngleFromRadians(mRadians);
    ;//默认角度
    private float circleRotate = 40;//小圆半径
    float radius;
    float smallLength;
    float largeLength;
    float downX;
    float downY;
    float moveX;
    float moveY;
    private int MODEL = 0;//默认第一种模式，总共应该目前是有4种，/0送风,1制冷,2制热,3除湿
    private int MaxValue = 30;//设置的最大值得最小值
    private int MinValue = 16;
    private int gray_line_number = MaxValue - MinValue;//灰条纹个数
    private int MaxAngle = 210;//最大角度
    private float[] pointerPosition;//外部圆圆心

    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme()
                .obtainStyledAttributes(
                        attrs,
                        R.styleable.CustomView
                        , 0, 0);

        try {
            mBorderColor = typedArray.getColor(R.styleable.CustomView_border_color, 0xff000000);
            mBorderWidth = typedArray.getDimension(R.styleable.CustomView_border_width, 2);
        } finally {
            typedArray.recycle();
        }

        init();
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setColor(mBorderColor);

        //刻度画笔
        rulerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rulerPaint.setStyle(Paint.Style.STROKE);
        rulerPaint.setStrokeWidth(mBorderWidth);
        rulerPaint.setColor(rulerColor);
        bitmapBlue = BitmapFactory.decodeResource(getResources(),
                R.mipmap.ac_view_background);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBounds = new RectF(getLeft(), getTop(), getRight(), getBottom());

        width = mBounds.right - mBounds.left;
        height = mBounds.bottom - mBounds.top;

        if (width < height) {
            radius = width / 4;
        } else {
            radius = height / 4;
        }

        smallLength = 10;
        largeLength = 20;
        pointerPosition = calculatePointerPosition(mRadians);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(bitmapBlue, null, new RectF(mBounds.centerX()-radius, mBounds.centerY()-radius, mBounds.centerX()+radius,  mBounds.centerY()+radius), null);
        canvas.save();
        mPaint.setColor(0x66555555);
        canvas.drawRoundRect(new RectF(mBounds.centerX() - (float) 0.9 * width / 2, mBounds.centerY() - (float) 0.9 * height / 2, mBounds.centerX() + (float) 0.9 * width / 2, mBounds.centerY() + (float) 0.9 * height / 2), 30, 30, mPaint);
        mPaint.setColor(mBorderColor);
        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), radius, mPaint);
        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), radius * 7 / 8, rulerPaint);//刻度尺圆

        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), circleRotate, mPaint);//中心圆

        //  添加游标 游标为圆形
        canvas.drawCircle(mBounds.centerX() + pointerPosition[0], mBounds.centerY() + pointerPosition[1], radius / 5, mPaint);//外部圆
        // 添加游标上的文字
        Paint pai = new Paint();
        pai.setColor(Color.WHITE);
        pai.setTextSize((int) (45 * 1));
        pai.setAntiAlias(true);
        canvas.drawText(timeRotate + "°", (mBounds.centerX() + pointerPosition[0]), (mBounds.centerY() + pointerPosition[1]),
                pai);
        //画刻度
        canvas.rotate(270, mBounds.centerX(), mBounds.centerY());//原来是水平开始画的，现在希望是从顶端开始画
        float start_x, start_y;
        float end_x, end_y;
        for (int i = 0; i <= gray_line_number; ++i) {
            start_x = radius * 7 / 8 * (float) Math.cos(Math.PI / 180 * i * MaxAngle / gray_line_number);
            start_y = radius * 7 / 8 * (float) Math.sin(Math.PI / 180 * i * MaxAngle / gray_line_number);
            if (i == ((MaxAngle / gray_line_number) >> 1) || i == 0 || i == gray_line_number) {
                end_x = start_x - radius * 7 / 8 / 5 * (float) Math.cos(Math.PI / 180 * i * MaxAngle / gray_line_number);
                end_y = start_y - radius * 7 / 8 / 5 * (float) Math.sin(Math.PI / 180 * i * MaxAngle / gray_line_number);
            } else {
                end_x = start_x - largeLength * (float) Math.cos(Math.PI / 180 * i * MaxAngle / gray_line_number);
                end_y = start_y - largeLength * (float) Math.sin(Math.PI / 180 * i * MaxAngle / gray_line_number);
            }
            start_x += mBounds.centerX();
            end_x += mBounds.centerX();
            start_y += mBounds.centerY();
            end_y += mBounds.centerY();
            canvas.drawLine(start_x, start_y, end_x, end_y, rulerPaint);
        }
//        canvas.rotate(timeRotate, mBounds.centerX(), mBounds.centerY());
//        canvas.drawLine(mBounds.centerX(), mBounds.centerY(), mBounds.centerX(), mBounds.centerY() - radius, mPaint);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getX();
                moveY = event.getY();

                //计算手指和圆心的与水平方向的弧度
                mRadians = (float) java.lang.Math.atan2(moveY - mBounds.centerY(), moveX - mBounds.centerX());
                Log.d("mRadians", "" + mRadians);
                timeRotate = calculateAngleFromRadians(mRadians);
                if (timeRotate < MaxAngle) {
                    pointerPosition = calculatePointerPosition(mRadians);
                } else {
                    timeRotate = MaxAngle;
                }

                invalidate();

                break;
        }
        return true;
    }

    private float[] calculatePointerPosition(float radians) {
        // if (calculateRadiansFromAngle(angle) > end_wheel)
        // angle = calculateAngleFromRadians(end_wheel);
        float x = (float) ((radius) * Math.cos(radians));
        float y = (float) ((radius) * Math.sin(radians));

        return new float[]{x, y};
    }

    //通过弧度计算角度
    private int calculateAngleFromRadians(float radians) {
        float unit = (float) (radians / (2 * Math.PI));
        if (unit < 0) {
            unit += 1;
        }
        int angle = (int) ((unit * 360) - ((360 / 4) * 3));
        if (angle < 0)
            angle += 360;
        return angle;
    }

    //通过角度计算弧度
    private float calculateRadiansFromAngle(float angle) {
        return (float) (((angle + 270) * (2 * Math.PI)) / 360);
    }

}
